package com.action.app_center;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.json.JSONArray;
import org.json.JSONObject;

import com.QRcodeBuilder.NetQrPCEntity;
import com.QRcodeBuilder.NetQrVo;
import com.dataProcess.MapPart;
import com.dataProcess.Matter;
import com.dataProcess.NetConnect;
import com.enetiy.Area;
import com.enetiy.Areainfo;
import com.enetiy.Areaip;
import com.enetiy.Balance;
import com.enetiy.Keeptime;
import com.enetiy.Useraccount;
import com.enetiy.Userinfo;
import com.enetiy.Users;
import com.enetiy.Vipmember;
import com.operation.HttpSendUrl;
import com.operation.NET;
import com.service.AreaIpService;
import com.service.AreaService;
import com.service.AreainfoService;
import com.service.AreausbleService;
import com.service.BindingService;
import com.service.ComboService;
import com.service.DealService;
import com.service.MapService;
import com.service.NetLinkService;
import com.service.UseraccountService;
import com.service.UserinfoService;
import com.threads.NetoutThread;
import com.util.DataUtil;
import com.util.Log4gUtil;
import com.util.NetTimeUtil;
import com.util.Verify;
import com.util.dataFormatUtil;

public class NetworkAction {
	private int siteAreaid;
	private int areaid;
	
	private String ip;
	private int userid;
	private String params;
	
	private String account;
	private String accountPwd;
	private int planTime;
	private String sessionid;
	
	private String apMac;
	private String apName;
	private String nasid;
	private int visitType;
	
	
	private NET net = new NET();
	
	private AreaService areaService;
	private UserinfoService userinfoService;
	private ComboService comboService;
	private AreainfoService areainfoService;
	private BindingService bindingService;
	private AreausbleService areausbleService;
	private UseraccountService useraccountService;
	private DealService dealService;
	private AreaIpService areaIpService;
	private NetLinkService netLinkService;
	private MapService mapService;
	
	private String gateIp;
	private String mac;
	private  int areaiden;
	private String token;
	private String NF_callid;
	private int wayid;
	
	
	private void verify(String params){
		if(params!=null&&!params.equals("")){
			JSONObject dataParser = DataUtil.dataParser(params);
			if(dataParser.has("areaid"))
				areaid= dataParser.getInt("areaid");
			if(dataParser.has("ip"))
				ip = dataParser.getString("ip");
			if(dataParser.has("userid"))
				userid = dataParser.getInt("userid");
			if(dataParser.has("mac"))
				mac = dataParser.getString("mac");
			if(dataParser.has("planTime"))
				planTime = dataParser.getInt("planTime");
			if(dataParser.has("wayid"))
				wayid = dataParser.getInt("wayid");
			if(dataParser.has("apMac"))
				apMac = dataParser.getString("apMac");
			if(dataParser.has("visitType"))
				visitType = dataParser.getInt("visitType");
		}
	}
	
	private int getSiteAreaid() throws Exception{
		int areaid = siteAreaid;
		if((apMac!=null&&!apMac.equals("")&&!apMac.equals("null"))
				||(apName!=null&&!apName.equals("")&&!apName.equals("null"))
				||(nasid!=null&&!nasid.equals("")&&!nasid.equals("null"))){
			MapPart mp = new MapPart();
			Log4gUtil.Info("getSiteAreaid-", userid+"", "进入连网param&area:"+siteAreaid, "apName--"+apName+"--apMac--"+apMac+"--nasid--"+nasid, NF_callid, token);
			areaid = mp.getAreaId(mapService, 0, apName, 10000, "", apMac, nasid);
		}
		return areaid;
	}
	
	/**
	 * 连接网络(直接)  >Nf007
	 */
	
	public void linkedNet(){
		long startTime = new Date().getTime();
		verify(params);
		net.sf.json.JSONObject sendJson = DataUtil.sendJson();
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = null;
		Area siteArea = null;
		Userinfo userinfo = null;
		try {
			if(siteAreaid==10000||siteAreaid==10005)
				siteAreaid = getSiteAreaid();//获取准确区域id
			
			Log4gUtil.Info("linkedNet-直接连网", userid+"", "进入连网param&area:"+siteAreaid, params, NF_callid, token);
			
			out = response.getWriter();
			//公网ip地址
			NetConnect netConnect = new NetConnect(0,0,0,0,0);
			//经过内网, 区域Id可用,并用户存在 
			if(siteAreaid!=0&&(siteArea = Verify.getArea(siteAreaid, areaService)) != null&&
					(userinfo = userinfoService.selectUserinfo(userid)) != null){
					gateIp = areaIpService.selectAreaip(siteAreaid).getPublicIp();
					
					if(siteArea.getId()==10005){
						Log4gUtil.Info("getNetConnect", userinfo.getUsers().getId()+"", "异步下线", "上网前下线--"+siteArea.getId(), "", token);
						//异步下线
						NetoutThread lt = new NetoutThread( userinfo, account,mapService, dealService,areaIpService,userinfo.getUsers().getId(), "", token);
						Thread t1 = new Thread(lt); 
						t1.start();
					}
					
					//账号
					List<Useraccount> accounts = useraccountService.selectUseraccounts(userinfo.getUsers().getId(), 1);
					if(!accounts.isEmpty())
						//连网 || 检查权限
						netConnect.getNetConnect(userinfo, siteArea, ip, gateIp, mac,accounts.get(0).getAccount(),visitType,comboService, 
								areainfoService, bindingService, areausbleService,userinfoService,dealService,NF_callid,token,
								apMac,mapService,netLinkService);
					}
			sendJson.put("code",1);
			sendJson.put("siteAreaid",siteAreaid);
			//网络连接提示
			sendJson.put("connect",netConnect.connect);
			//套餐
			sendJson.put("comboHimt", netConnect.comboHimt);//暂时未用
			sendJson.put("combonum", netConnect.comboid);
			//会员
			sendJson.put("vipHimt",netConnect.vipHimt);
			sendJson.put("vipnum", netConnect.vipid);
			//模式
			JSONArray way = new JSONArray(netConnect.way);
			sendJson.put("pattern", way.toString());
			
			//绑定
			sendJson.put("bandingHimt", netConnect.bandingHimt);
			sendJson.put("networkingTime", netConnect.bandinglongTime);//可上网时间
			//二维码
			sendJson.put("erweimaHimt", netConnect.erweimaHimt);
			//首次免费
			net.sf.json.JSONObject j = DataUtil.sendJson();
			j.put("firstfree", netConnect.firstfreeHimt);
			j.put("freetime",dataFormatUtil.getHour(netConnect.freeTime));
			j.put("areaiden", netConnect.freeIden);
			
			
			sendJson.put("firstfreeHimt", j);
		} catch (Exception e) {
			sendJson.put("code", -1);
			sendJson.put("msg", "服务器异常");
			e.printStackTrace();
		}
		//发送
		out.print(sendJson);
		Long endTime = new Date().getTime();
		Log4gUtil.Info("linkedNet-直接连网", userid+"", "直接连网时间", (startTime-endTime)+"-结果-"+sendJson.toString(), NF_callid, token);
	}
	
	

	/**
	 * 
	 * 首次免费上网  >Nf011
	 */
	public void timingNet(){
		if(params!=null&&!params.equals("")){
			JSONObject dataParser = DataUtil.dataParser(params);
			if(dataParser.has("ip"))
				ip = dataParser.getString("ip");
			if(dataParser.has("userid"))
				userid = dataParser.getInt("userid");
			if(dataParser.has("mac"))
				mac = dataParser.getString("mac");
			if(dataParser.has("areaiden"))
				areaiden = dataParser.getInt("areaiden");
		}
		
		net.sf.json.JSONObject sendJson = DataUtil.sendJson();
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = null;
		
		try {
			if(siteAreaid==10000||siteAreaid==10005)
				siteAreaid = getSiteAreaid();//获取准确区域id
			
			Log4gUtil.Info("timingNet-定时连网", userid+"", "进入param", params+siteAreaid, NF_callid, token);
			
			out = response.getWriter();
			//公网ip地址
			gateIp = areaIpService.selectAreaip(siteAreaid).getPublicIp();
			Areainfo areainfo = areaService.selsctAreainfo(siteAreaid, areaiden);
			Userinfo userinfo = userinfoService.selectUserinfo(userid);
			List<Useraccount> accounts = useraccountService.selectUseraccounts(userid, 1);
			if(areainfo!=null&&userinfo!=null&&!accounts.isEmpty()){
				String time = NetTimeUtil.getTimeUtil(new Date(),areainfo.getValidTime());
				//连网
				int netAllow = net.netAllow("","", siteAreaid, ip, areainfo.getAreatype().getBanfwidth(), areainfo.getArea().getNetOnline(),
						time, gateIp, userinfo.getUsers().getId(), mac,accounts.get(0).getAccount(),1,1,NF_callid,token,
						apMac,"",mapService,areausbleService,netLinkService);
			
				Keeptime keeptime = new Keeptime(areainfo.getArea(), userinfo.getUsers(), time, 2,1,10,"");
				dealService.insertKeeptime(keeptime);
				userinfo.setNetTime(time);
				userinfo.setBinding(null);
				userinfo.setComboinfo(null);
				userinfo.setNetOnline(1);
				userinfo.setArea(areainfo.getArea());
				userinfo.setUserIp(ip);
				userinfo.setUserMac(mac);
				
				userinfoService.updateUserinfo(userinfo);
				sendJson.put("code", 1);
				sendJson.put("connect", netAllow);
			}else{
				sendJson.put("code",0);
				sendJson.put("msg", "入口数据有误");
			}
		} catch (Exception e) {
			sendJson.put("code", -1);
			sendJson.put("msg", "服务器异常");
			e.printStackTrace();
		}
		out.print(sendJson);
		Log4gUtil.Info("timingNet-定时连网", userid+"", "输出", sendJson.toString(), NF_callid, token);
	}
	
	/**
	 * 会员连接网络  >Nf008
	 */
	public void planNet(){
		verify(params);
		
		net.sf.json.JSONObject sendJson = DataUtil.sendJson();
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = null; 
		
		try {
			if(siteAreaid==10000||siteAreaid==10005)
				siteAreaid = getSiteAreaid();//获取准确区域id
			
			Log4gUtil.Info("planNet-预约上网", userid+"", "进入params", params+"siteAreaid:"+siteAreaid, NF_callid, token);
			out = response.getWriter();
			//公网ip地址
			Userinfo userinfo = userinfoService.selectUserinfo(userid);
			List<Useraccount> accounts = useraccountService.selectUseraccounts(userid, 1);
			//Areainfo areainfo = areainfoService.selectAreainfo(siteAreaid, "NF-net-20");
			
			Area area = areaService.selectArea(siteAreaid);
			Balance bal = dealService.selectBalance(userid);
			
			if(userinfo!=null&&!accounts.isEmpty()){
				if(area!=null){
					gateIp = areaIpService.selectAreaip(siteAreaid).getPublicIp();
					if(area.getId()==10005){
						//异步下线
						NetoutThread lt = new NetoutThread( userinfo, account,mapService, dealService,areaIpService,userinfo.getUsers().getId(), "", token);
						Thread t1 = new Thread(lt); 
						t1.start();
					}
					
					List<Vipmember> vip = dealService.selectVipmember(userid);
					if(vip!=null&&!vip.isEmpty()){
						if(bal!=null&&(bal.getLongTime()+bal.getTicket04())>=0){
							int netAllow = 0;
							if(planTime>0){
								netAllow = net.netAllow("","", siteAreaid, ip, 8,area.getNetOnline(),
										NetTimeUtil.getTimeUtil(new Date(),planTime), gateIp,userid, mac,accounts.get(0).getAccount(),5,1,NF_callid,token,
										apMac,"",mapService,areausbleService,netLinkService);
							}else{
								netAllow = net.netAllow("","", siteAreaid, ip, 8, area.getNetOnline(),
										null, gateIp,userid, mac,accounts.get(0).getAccount(),5,1,NF_callid,token,
										apMac,"",mapService,areausbleService,netLinkService);
							}
							
							if(netAllow==1){
								Keeptime kee = dealService.selectKeeptime(userid, 5,1,siteAreaid);
								if(kee!=null){
									kee.setPlanTime(planTime);
									kee.setKeepType(5);
									kee.setKeepDevice(1);
									kee.setArea(area);
									kee.setNetOrder(net.netOrder);
									kee.setKeepTime(NetTimeUtil.getTimeUtil(new Date(),planTime));
									dealService.updateKeeptime(kee);
								}else{
									kee= new Keeptime();
									Users users = new Users();
									users.setId(userid);
									kee.setUsers(users);
									
									kee.setKeepTime(NetTimeUtil.getTimeUtil(new Date(),planTime));
									kee.setKeepType(5);
									kee.setNetOrder(net.netOrder);
									kee.setPlanTime(planTime);
									kee.setKeepDevice(1);
									kee.setArea(area);
									dealService.insertKeeptime(kee);
								}
								userinfo.setNetOnline(1);
								userinfo.setUserIp(ip);
								userinfo.setUserMac(mac);
								userinfo.setArea(area);
								userinfoService.updateUserinfo(userinfo);
								sendJson.put("code", 1);
								sendJson.put("connect", netAllow);
							}else{
								sendJson.put("code", -1);
								sendJson.put("msg", "连网服务器异常");
							}
						}else{
							sendJson.put("code", 0);
							sendJson.put("reason", 1);
							sendJson.put("msg", "余额不足");
						}
					}else{
						sendJson.put("code", 0);
						sendJson.put("reason", 3);
						sendJson.put("msg", "您不是会员");
					}
				}else{
					sendJson.put("code", 0);
					sendJson.put("reason", 4);
					sendJson.put("msg", "此区域不能会员上网");
				}
			}else{
				sendJson.put("code", 0);
				sendJson.put("reason", 2);
				sendJson.put("msg", "入口数据有误");
			}
		} catch (Exception e) {
			sendJson.put("code", -1);
			sendJson.put("msg", "服务器异常");
			e.printStackTrace();
		}
		out.print(sendJson);
		Log4gUtil.Info("planNet-预约上网", userid+"", "输出", sendJson.toString(), NF_callid, token);
	}
	
	/**
	 * 会员预约时间内 连接网络  >Nf061
	 */
	public void planInNet(){
		verify(params);
		net.sf.json.JSONObject sendJson = DataUtil.sendJson();
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = null; 
		try {
			if(siteAreaid==10000||siteAreaid==10005)
				siteAreaid = getSiteAreaid();//获取准确区域id
			Log4gUtil.Info("planInNet-预约时间内上网", userid+"", "进入params", params+"siteAreaid:"+siteAreaid, NF_callid, token);
		
			out = response.getWriter();
			Userinfo userinfo = userinfoService.selectUserinfo(userid);
			Keeptime keeptime = dealService.selectKeeptime(userid, 5,1,siteAreaid);
			Area selectArea = areaService.selectArea(siteAreaid);
			
			if(keeptime!=null&&NetTimeUtil.getPareTimeUtil(keeptime.getKeepTime(), "yyyy-MM-dd HH:mm:ss")){
				 Useraccount useraccount = useraccountService.selectUseraccount(userid, 1);
				if(userinfo!=null&&useraccount!=null&&selectArea!=null){
						gateIp = areaIpService.selectAreaip(siteAreaid).getPublicIp();
						
						if(userinfo.getArea().getId()>=10000&&userinfo.getArea().getId()!=10003&&userinfo.getArea().getId()!=10004){
							//异步下线
							NetoutThread lt = new NetoutThread( userinfo, account,mapService, dealService,areaIpService,userinfo.getUsers().getId(), "", token);
							Thread t1 = new Thread(lt); 
							t1.start();
						}
						
							int netAllow = net.netAllow("","", siteAreaid, ip, 8, selectArea.getNetOnline(),
									keeptime.getKeepTime(), gateIp,userid, mac,useraccount.getAccount(),5,1,NF_callid,token,
									apMac,keeptime.getNetOrder(),mapService,areausbleService,netLinkService);
							if(netAllow==1){
								userinfo.setNetOnline(1);
								userinfo.setUserIp(ip);
								userinfo.setUserMac(mac);
								userinfo.setArea(selectArea);
								userinfoService.updateUserinfo(userinfo);
								keeptime.setArea(selectArea);
								dealService.updateKeeptime(keeptime);
								
								sendJson.put("code", 1);
								sendJson.put("connect", netAllow);
							}else{
								sendJson.put("code", -1);
								sendJson.put("msg", "连网服务器异常");
							}
				}else{
					sendJson.put("code", 0);
					sendJson.put("reason", 2);
					sendJson.put("msg", "入口数据有误");
				}
			}else{
				sendJson.put("code", 0);
				sendJson.put("reason", 1);
				sendJson.put("msg", "不在预约时间内");
			}
		} catch (Exception e) {
			sendJson.put("code", -1);
			sendJson.put("msg", "服务器异常");
			e.printStackTrace();
		}
		out.print(sendJson);
		Log4gUtil.Info("planInNet-预约时间内上网", userid+"", "输出", sendJson.toString(), NF_callid, token);
	}
	
	/**
	 * 
	 * 会员续时长 >Nf062
	 */
	public void planAdd(){
		verify(params);
		net.sf.json.JSONObject send = DataUtil.sendJson();
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = null; 
		try {
			if(siteAreaid==10000||siteAreaid==10005)
				siteAreaid = getSiteAreaid();//获取准确区域id
			
			Log4gUtil.Info("planAdd-会员续时长", userid+"", "进入params", params+"siteAreaid:"+siteAreaid, NF_callid, token);
			
			out = response.getWriter();
			Areainfo areainfo = areainfoService.selectAreainfo(siteAreaid, "NF-net-"+wayid);
			
			Userinfo userinfo = userinfoService.selectUserinfo(userid);
			
			Keeptime keeptime = dealService.selectKeeptime(userid,5,1,siteAreaid);
			if(areainfo!=null&&userinfo!=null){
				gateIp = areaIpService.selectAreaip(siteAreaid).getPublicIp();
				boolean pareTimeUtil = NetTimeUtil.getPareTimeUtil(keeptime.getKeepTime(), "yyyy-MM-dd HH:mm:ss");
				String netOrder = keeptime.getNetOrder();
				if(keeptime!=null&&pareTimeUtil&&netOrder!=null&&!netOrder.equals("")&&!netOrder.equals("empty")){
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");	
					Date parse = sdf.parse(keeptime.getKeepTime());
					String timeUtil = NetTimeUtil.getTimeUtil(parse, planTime);
					//发送
					HttpSendUrl sendUrl = new HttpSendUrl();
					
					String url = "http://"+gateIp+areainfo.getOnlineUrl();
					String param = "protectid="+siteAreaid+"&orderid="+netOrder+"&end_time="+timeUtil+"&userid="+userid+"&servertype=5";
					String sendRequest = sendUrl.getSendRequest(url, param, "POST", 6000);
					//解析
					int result = 0;
					if(!sendRequest.contains("****")){
						JSONObject js = new JSONObject(sendRequest); 
						if(js.has("code"))
							result = js.getInt("code");
					}
					//结果
					if(result==1){
						keeptime.setKeepTime(timeUtil);
						keeptime.setArea(areainfo.getArea());
						keeptime.setPlanTime(keeptime.getPlanTime()+planTime);
						dealService.updateKeeptime(keeptime);
						send.put("code", 1);
					}else{
						send.put("code",0);
						send.put("reason", 1);
						send.put("msg", "续时服务异常");
					}
				}else{
					send.put("code", 0);
					send.put("reason", 3);
					send.put("msg", "时长已过,不能续时");
				}
			}else{
				send.put("code", 0);
				send.put("reason", 2);
				send.put("msg", "入口数据有误");
			}
		} catch (Exception e) {
			send.put("code", -1);
			send.put("msg", "服务器异常");
			e.printStackTrace();
		}
		out.print(send);
		Log4gUtil.Info("planAdd-续时", userid+"", "输出", send.toString(), NF_callid, token);
	}
	
	/**
	 * 验证连网(目前深大) >Nf009
	 */
	public void verifyNet(){
		if(params!=null&&!params.equals("")){
			JSONObject dataParser = DataUtil.dataParser(params);
			if(dataParser.has("ip"))
				ip = dataParser.getString("ip");
			if(dataParser.has("userid"))
				userid = dataParser.getInt("userid");
			if(dataParser.has("account"))
				account = dataParser.getString("account");
			if(dataParser.has("accountPwd"))
				accountPwd = dataParser.getString("accountPwd");
			if(dataParser.has("mac"))
				mac = dataParser.getString("mac");
			
			if(dataParser.has("sessionid"))
				sessionid = dataParser.getString("sessionid");
		}
		
		Log4gUtil.Info("verifyNet-验证连网", userid+"", "进入params", params, NF_callid, token);
		
		net.sf.json.JSONObject sendJson = DataUtil.sendJson();
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = null;
		try {
			out = response.getWriter();
			//二维码
			boolean flg = true;
			NetQrVo netQrVo = NetQrVo.getNetQrVo();
			NetQrPCEntity pc = netQrVo.getNetQrPCEntityMap().get(sessionid);
			if(sessionid!=null&&!sessionid.equals("")){
				if(pc!=null){
					mac = pc.getMac();
					ip = pc.getIp();
					siteAreaid = pc.getSiteAreaid();
				}else{
					sendJson.put("code",0);
					sendJson.put("reason",2);
					sendJson.put("msg", "缓存不存在");
					flg = false;
				}
			}
			
		if(flg){
				Area area = areaService.selectArea(siteAreaid);
				Userinfo userinfo = userinfoService.selectUserinfo(userid);
				Areaip areaip = areaIpService.selectAreaip(siteAreaid);
				Useraccount selectUseraccount = useraccountService.selectUseraccount(userid, 1);
				int connect = -1;
				NET net = new NET();
				if(area!=null&&userinfo!=null&&areaip!=null&&selectUseraccount!=null){
					String gateip = areaip.getPublicIp();
					//连网
					connect = net.netAllow(account, accountPwd, siteAreaid, ip, 4,
							area.getNetOnline(),"",
							gateip,userid,mac,selectUseraccount.getAccount(),3,1,NF_callid,token,
							apMac,"",mapService,areausbleService,netLinkService);
					if(connect==1){
						userinfo.setNetOnline(1);
						userinfo.setUserMac(mac);
						userinfo.setUserIp(ip);
						userinfoService.updateUserinfo(userinfo);
						if(sessionid!=null&&!sessionid.equals("")){	
							pc.setShow("self");
							//修改缓存
							netQrVo.getNetQrPCEntityMap().put(sessionid, pc);
						}
					}
					sendJson.put("code", 1);
					sendJson.put("connect",connect);
				}else{
					sendJson.put("code",0);
					sendJson.put("reason",1);
					sendJson.put("msg", "数据有误");
				}
			}
		} catch (Exception e) {
			sendJson.put("code", -1);
			e.printStackTrace();
		}
		out.print(sendJson);
		Log4gUtil.Info("verifyNet-验证连网", userid+"", "输出", sendJson.toString(), NF_callid, token);
	}
	
	
	
	
	
	
	/**
	 * 
	 * 网络下线  >Nf013
	 */
	public void offNet(){
		if(params!=null&&!params.equals("")){
			JSONObject dataParser = DataUtil.dataParser(params);
			if(dataParser.has("userid")) 
				userid = dataParser.getInt("userid");
			if(dataParser.has("ip"))
				ip = dataParser.getString("ip");
			if(dataParser.has("mac"))
				mac = dataParser.getString("mac");
			if(dataParser.has("siteAreaid"))
				siteAreaid = dataParser.getInt("siteAreaid");
		}
		
		net.sf.json.JSONObject sendJson = DataUtil.sendJson();
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = null;
		try {
			if(siteAreaid==10000||siteAreaid==10005)
				siteAreaid = getSiteAreaid();//获取准确区域id
			Log4gUtil.Info("offNet-网络下线", userid+"", "进入params", params, NF_callid, token);
			
			out = response.getWriter();
			Userinfo userinfo = userinfoService.selectUserinfo(userid);
			Area area = areaService.selectArea(siteAreaid);
			int outnet =0;
			NET net = new NET();
			if(area!=null){
				
				Areaip areaip =null;
				if(userinfo!=null&&userinfo.getUserIp()!=null&&userinfo.getArea()!=null){
					areaip = areaIpService.selectAreaip(siteAreaid);
					gateIp = areaip.getPublicIp();
					
					List<Useraccount> accounts = useraccountService.selectUseraccounts(userinfo.getUsers().getId(), 1);
					if(!accounts.isEmpty()){
						
						String netorder="";
						Keeptime keeptime = dealService.selectKeeptime(userid, 5, 1, siteAreaid);
						if(keeptime!=null&&keeptime.getKeepTime()!=null&&keeptime.getNetOrder()!=null){
							if(NetTimeUtil.getDurationUtil(new Date(), keeptime.getKeepTime())>-10){
								netorder = keeptime.getNetOrder();
							}
						}
						outnet = net.netIntercept(siteAreaid, ip, area.getNetOut(), gateIp,
								mac,userid,accounts.get(0).getAccount(),NF_callid,token,netorder);
						Log4gUtil.getLog().info("offNet-userid-"+userid+"网络下线-下线结果:"+outnet);
						if(outnet==1){
							userinfo.setNetOnline(0);
							Matter matter = new Matter();
							matter.feeYue(dealService,mapService, userid, net.time,NF_callid,token,net.server_type,net.mac_type,net.protectid,net.orderid);
							userinfoService.updateUserinfo(userinfo);
							sendJson.put("code", 1);
						}else{
							sendJson.put("code", 0);
							sendJson.put("msg", "网络下线未成功");
						}
					}
				}else{
					areaip = areaIpService.selectAreaip(siteAreaid);
					gateIp = areaip.getPublicIp();
					
					outnet = net.netIntercept(siteAreaid, ip, area.getNetOut(), gateIp,
							mac,0,"",NF_callid,token,"");
					if(outnet==1)
						sendJson.put("code", 1);
					else
						sendJson.put("code", 0);
				}
			}else{
				sendJson.put("code", 0);
				sendJson.put("msg", "区域不存在");
			}
		} catch (Exception e) {
			sendJson.put("code", -1);
		}
		Log4gUtil.Info("offNet-网络下线", userid+"", "输出", sendJson.toString(), NF_callid, token);
		out.print(sendJson);
	}
	
	
		public void setSiteAreaid(int siteAreaid) {
			this.siteAreaid = siteAreaid;
		}
		public void setAreaid(int areaid) {
			this.areaid = areaid;
		}
		public void setIp(String ip) {
			this.ip = ip;
		}
		public void setUserid(int userid) {
			this.userid = userid;
		}
		public void setParams(String params) {
			this.params = params;
		}
		public void setAreaService(AreaService areaService) {
			this.areaService = areaService;
		}
		public void setUserinfoService(UserinfoService userinfoService) {
			this.userinfoService = userinfoService;
		}
		public void setComboService(ComboService comboService) {
			this.comboService = comboService;
		}
		public void setAreainfoService(AreainfoService areainfoService) {
			this.areainfoService = areainfoService;
		}
		public void setBindingService(BindingService bindingService) {
			this.bindingService = bindingService;
		}
		public void setAreausbleService(AreausbleService areausbleService) {
			this.areausbleService = areausbleService;
		}


		public void setGateIp(String gateIp) {
			this.gateIp = gateIp;
		}


		public void setAccount(String account) {
			this.account = account;
		}


		public void setAccountPwd(String accountPwd) {
			this.accountPwd = accountPwd;
		}


		public void setUseraccountService(UseraccountService useraccountService) {
			this.useraccountService = useraccountService;
		}


		public void setMac(String mac) {
			this.mac = mac;
		}


		public void setAreaiden(int areaiden) {
			this.areaiden = areaiden;
		}

		public void setPlanTime(int planTime) {
			this.planTime = planTime;
		}

		public void setDealService(DealService dealService) {
			this.dealService = dealService;
		}

		public void setAreaIpService(AreaIpService areaIpService) {
			this.areaIpService = areaIpService;
		}

		public void setToken(String token) {
			this.token = token;
		}

		public void setNF_callid(String nF_callid) {
			NF_callid = nF_callid;
		}

		public void setNetLinkService(NetLinkService netLinkService) {
			this.netLinkService = netLinkService;
		}
		public void setMapService(MapService mapService) {
			this.mapService = mapService;
		}
		public void setApMac(String apMac) {
			this.apMac = apMac;
		}
		public void setWayid(int wayid) {
			this.wayid = wayid;
		}
		public void setApName(String apName) {
			this.apName = apName;
		}
		public void setNasid(String nasid) {
			this.nasid = nasid;
		}

		public void setSessionid(String sessionid) {
			this.sessionid = sessionid;
		}

		public void setVisitType(int visitType) {
			this.visitType = visitType;
		}

		
		
	}
		
	
	
